﻿using Newtonsoft.Json;

namespace Server
{
    public class ResServerTime
    {
        private static int uid = 0;
        [JsonIgnore] public uint packetLength;
        [JsonIgnore] public byte operationType;
        [JsonIgnore] public byte zipFlag;
        [JsonIgnore] public int uniqueId;
        [JsonIgnore] public int id = 655390;
        public long time;
        public int errorCode;

        public ResServerTime()
        {
            uid++;
            packetLength = sizeof(uint) + sizeof(byte) + sizeof(byte) + sizeof(int) + sizeof(int);
            operationType = 1;
            zipFlag = 0;
            uniqueId = uid;
            id = 655390;
            time = uid++;
            errorCode = 0;
        }
        
        public byte[] Serialize()
        {
            var offset = 0;
            var data = JsonSerializerHelper.Serialize<ResServerTime>(this);
            packetLength += (uint)data.Length;

            var buffer = new byte[packetLength];
            buffer.WriteUInt(packetLength, ref offset);
            buffer.WriteByte(operationType, ref offset);
            buffer.WriteByte(zipFlag, ref offset);
            buffer.WriteInt(uniqueId, ref offset);
            buffer.WriteInt(id, ref offset);
            buffer.WriteBytesWithoutLength(data, ref offset);
            return buffer;
        }
    }
}